package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.config.RedisConfig;
import com.itheima.reggie.pojo.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        //获取对象中的手机号
        String phone = user.getPhone();
        //判断账号是否被禁用
//        QueryWrapper<User> queryWrapper =new QueryWrapper<>();
//        queryWrapper.eq("phone",phone);
//        User user1 = userService.getOne(queryWrapper);
//        if(user1.getStatus()==0){
//            return R.error("用户账户被封");
//        }
        //生成一个四位数的随机验证码
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        log.info("验证码是:{}",code);
        //发送短信
//        SMSUtils.sendMessage("传智健康", "code_1234", phone, code);
        //将需要生成的验证码保存到session中
//        session.setAttribute(phone,code);

        //将验证码保存到redis中 并设置过期时间
        redisTemplate.opsForValue().set(phone,code,2, TimeUnit.MINUTES);
        return R.success("短信发送成功");
    }

    @PostMapping("/login")
    public R<User> login(@RequestBody Map<String,String> map,HttpSession session){
        //获取手机号验证码
        String phone = map.get("phone");
        String code1 = map.get("code");
        //对比验证码
//        String code2 = (String) session.getAttribute(phone);
        //从redis中获取缓存的验证码
        String code2 = (String) redisTemplate.opsForValue().get(phone);
        boolean flag = StringUtils.equals(code1, code2);
        if(!flag){
            return R.error("验证码错误");
        }
        //判断用户信息是否存在 如果存在则直接登录 如果不存在则系统自动注册登录
        QueryWrapper<User> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User user = userService.getOne(queryWrapper);
        if(user == null){
            //如果数据库查不到就证明是新用户 需要自动注册
            user = new User();
            user.setPhone(phone);
            //用户状态注册时默认为1  启用用户
            user.setStatus(1);
            userService.save(user);
        }
        //将用户存入session中
        session.setAttribute("user",user.getId());
        //登陆成功要删除redis中的验证码
        redisTemplate.delete(phone);
        return R.success(user);
    }
}
